package com.example.im.ui.fragment

import android.annotation.SuppressLint
import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.example.im.R
import com.example.im.callback.OnClickRoomListener
import com.example.im.ui.adapter.RoomUserAdapter
import com.example.im.common.Constants
import com.example.im.entity.FriendOrRoom
import com.example.im.mvp.base.BasePresenter
import com.example.im.ui.abstracts.AbstractBaseFragment
import com.example.im.ui.activity.RoomActivity
import com.example.im.utils.LogUtils
import kotlinx.android.synthetic.main.fragment_room_center.*
import kotlinx.android.synthetic.main.fragment_room_center.tv_name
import kotlinx.android.synthetic.main.fragment_room_center.tv_room_name
import org.jivesoftware.smackx.muc.Affiliate

class RoomCentreFragment : AbstractBaseFragment<BasePresenter<*>>(), View.OnClickListener,
    RoomActivity.OnToRoomCentreFragment {

    private var adapter: RoomUserAdapter? = null
    private var owners = mutableListOf<FriendOrRoom>()

    private var rooms: FriendOrRoom? = null

    private var mOnClickRoomListener: OnClickRoomListener? = null

    companion object {

        fun newInstance(room: FriendOrRoom): RoomCentreFragment {
            val bundle = Bundle()
            bundle.putSerializable(Constants.BUNDLE_T0_ROOM_DATA, room)
            val fragment = RoomCentreFragment()
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mOnClickRoomListener = context as OnClickRoomListener
    }

    override val layoutId: Int
        get() = R.layout.fragment_room_center

    override fun initView() {

        rooms = arguments!!.getSerializable(Constants.BUNDLE_T0_ROOM_DATA) as FriendOrRoom

        tv_name.text = rooms!!.nickname
        tv_room_name.text = rooms!!.account

        adapter = RoomUserAdapter(activity!!, mutableListOf())
        adapter!!.setOnItemClickListener { v, _ ->
            //mOnClickRoomListener!!.onClickUserAdd(v)

            val transaction = activity!!.supportFragmentManager.beginTransaction()
            transaction.add(R.id.frame_layout, RoomInviteFragment.newInstance(owners as ArrayList<FriendOrRoom>))
            transaction.addToBackStack(null)
            transaction.commit()
        }
        recycler_view.layoutManager = GridLayoutManager(activity!!, 5)
        recycler_view.adapter = adapter

        tv_room_user_total.setOnClickListener(this)
        tv_room_send_chat.setOnClickListener(this)
        tv_room_clear_chat.setOnClickListener(this)
        tv_room_clear_exit.setOnClickListener(this)

        mOnClickRoomListener!!.onInitAffiliate(rooms!!)


    }

    override fun initPresenter() {

    }

    override fun initLazyLoad() {
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.tv_room_user_total -> {
                mOnClickRoomListener!!.onClickUserTotal(v, owners as ArrayList<FriendOrRoom>)
            }
            R.id.tv_room_send_chat -> mOnClickRoomListener!!.onClickSendChat(v, rooms!!)
            R.id.tv_room_clear_chat -> mOnClickRoomListener!!.onClickClearChat(v, rooms!!)
            R.id.tv_room_clear_exit -> mOnClickRoomListener!!.onClickClearExit(v, rooms!!)
        }
    }

    override fun onRoomUser(owners: MutableList<FriendOrRoom>) {
        this.owners = owners
        tv_room_member_count.text = "共${owners.size}人"
        adapter!!.addData(owners.subList(0, if (owners.size >= 4) 4 else owners.size))
    }
}